package com.hongfei.domain.auth.service;


import com.google.common.cache.Cache;
import com.hongfei.domain.auth.adapter.port.ILoginPort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;

@Slf4j
@Service
public class WeixinLoginService implements ILoginService {

    @Resource
    private ILoginPort loginPort;
    @Resource
    private Cache<String, String> openidToken;

    @Override
    public String createQrCodeTicket() throws Exception {
        return loginPort.createQrCodeTicket();
    }

    @Override
    public String checkLogin(String ticket) {
        String accessToken = openidToken.getIfPresent(ticket);
        log.info("openidToken:{}", accessToken);
        return openidToken.getIfPresent(ticket);
    }
    //gQFn8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAycHVRNVZ3d2tlcEcxMlgxSE5FMUYAAgS7dANoAwQAjScA
    //gQE88DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyR21ONVVId2tlcEcxMGQ2SHhFMUUAAgQNeQNoAwQAjScA
    @Override
    public void saveLoginState(String ticket, String openid) throws IOException {
        // 保存登录信息
        openidToken.put(ticket, openid);
        String accessToken = openidToken.getIfPresent(ticket);
        log.info("openidToken:{}", accessToken);
        // 发送模板消息
        loginPort.sendLoginTemplate(openid);
    }

}
